import 'package:flutter/material.dart';
import '../sites/biquge.dart';
import '../../utils/navigator_utils.dart';

class LatestBookHome extends StatefulWidget {
  @override
  _LatestBookHomeState createState() => _LatestBookHomeState();
}

class _LatestBookHomeState extends State<LatestBookHome>
    with AutomaticKeepAliveClientMixin<LatestBookHome> {
  Future _future;

  @override
  void initState() {
    this._future = BiqugeSpider.getLatestBook();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return FutureBuilder(
          future: this._future,
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (snapshot.connectionState != ConnectionState.done) {
              return Center(
                child: CircularProgressIndicator(),
              );
            }
            List latestRecordBookList = snapshot.data["latestRecordBookList"];
            List latestUpdatebookList = snapshot.data["latestUpdatebookList"];
            return MediaQuery.removePadding(
                  removeTop: true,
                  context: context,
                  child: ListView(
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(left: 16, top: 10),
                          child: Text(
                            "最近更新",
                            style: TextStyle(
                                fontSize: 20, fontWeight: FontWeight.w300),
                          ),
                        ),
                        LastestUpdateBook(latestUpdatebookList),
                        Container(
                          margin: EdgeInsets.only(left: 16, top: 10),
                          child: Text(
                            "最近收录",
                            style: TextStyle(
                                fontSize: 20, fontWeight: FontWeight.w300),
                          ),
                        ),
                        LastestRecordBook(latestRecordBookList)
                      ],
                ));
          },
    );
  }

  @override
  bool get wantKeepAlive => true;
}

class LastestRecordBook extends StatelessWidget {
  final List latestRecordBookList;
  LastestRecordBook(this.latestRecordBookList);

  List<Widget> _buildContent(context) {
    return latestRecordBookList.map((item) {
      return Card(
          elevation: 0.1,
          child: ListTile(
            onTap: () {
              navToBookDetail(context, url: item["url"], title: item["title"]);
            },
            leading: Text(item["type"]),
            title:
                Text(item["title"], style: TextStyle(color: Colors.lightBlue)),
            trailing: Text(item["updateTime"]),
          ));
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: _buildContent(context));
  }
}

class LastestUpdateBook extends StatelessWidget {
  final List latestUpdatebookList;
  LastestUpdateBook(this.latestUpdatebookList);

  List<Widget> _buildContent(context) {
    return latestUpdatebookList.map((item) {
      return Card(
          elevation: 0.1,
          child: ListTile(
            onTap: () {
              navToBookDetail(context, url: item["url"], title: item["title"]);
            },
            leading: Text(item["type"]),
            title: Text(
              item["title"],
              style: TextStyle(color: Colors.lightBlue),
            ),
            subtitle: Text(item["author"] + "\n" + item["latestChapterName"]),
            trailing: Text(item["updateTime"]),
          ));
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: _buildContent(context));
  }
}
